package org.wlw.web.servlet;

import org.wlw.web.anno.Resource;
import org.wlw.web.models.Topic;
import org.wlw.web.models.User;
import org.wlw.web.service.IColumnService;
import org.wlw.web.service.ITopicService;
import org.wlw.web.utils.WebUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@WebServlet("/admin/topic")
public class TopicServlet extends BaseServlet{

    private ITopicService topicService;

    private IColumnService columnService;

    @Resource
    public void setColumnService(IColumnService columnService) {
        this.columnService = columnService;
    }

    @Resource
    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }

    public String index(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("topics",topicService.find(null));
        return "topic/list";
    }

    public String delete(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        topicService.delete(id);
        return getRedirectUrl("/admin/topic");
    }

    public String add(HttpServletRequest request,HttpServletResponse response) {
        request.setAttribute("cs",columnService.findCanAddTopic());
        return "topic/add";
    }

    public String update(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        request.setAttribute("cs",columnService.findCanAddTopic());
        request.setAttribute("topic",topicService.load(id));
        return "topic/update";
    }

    public String updateAction(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Topic t = topicService.load(id);
        t.setCid(Integer.parseInt(request.getParameter("cid")));
        t.setTitle(request.getParameter("title"));
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        t.setAuthor(loginUser.getNickname());
        t.setContent(request.getParameter("content"));
        String releaseDate = request.getParameter("releaseDate");
        t.setReleaseDate(WebUtils.strToDate(releaseDate));
        topicService.update(t);
        return getRedirectUrl("/admin/topic");
    }

    public String addAction(HttpServletRequest request,HttpServletResponse response) {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String releaseDate = request.getParameter("releaseDate");
        int cid = Integer.parseInt(request.getParameter("cid")) ;
        Topic t = new Topic();
        t.setCid(cid);
        t.setTitle(title);
        t.setContent(content);
        t.setCreateDate(new Date());
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        t.setAuthor(loginUser.getNickname());
        t.setReleaseDate(WebUtils.strToDate(releaseDate));
//        System.out.println(releaseDate);
//        t.setReleaseDate(releaseDate);
        topicService.add(t);
        return getRedirectUrl("/admin/topic");
    }
}
